import Vue from 'vue'
import { formatDate } from '../utils/time'

Vue.filter('formatDate', formatDate)

// 注册过滤器
Vue.filter('formatRatio', function (v) {
  // 返回处理后的值
  return parseInt(v * 1000, 10) / 10 + '%'
})

const polishDigits = (num) => {
  return num < 10 ? '0' + num : num
}
Vue.filter('formatTime', function (v) {
  // 返回处理后的值
  let timestemp = String(v).length === 10 ? v * 1000 : v
  let date = new Date(timestemp)
  let Y = date.getFullYear() + '-'
  let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
  let D = polishDigits(date.getDate()) + ' '
  let h = polishDigits(date.getHours()) + ':'
  let m = polishDigits(date.getMinutes()) + ':'
  let s = polishDigits(date.getSeconds())
  return Y + M + D + h + m + s
})

// 处理银行帐号  622202201444888888 => 6222 0220 1444 8888 88
Vue.filter('formatBankAccount', function (v) {
  let replaceSpaceArr = _.trim(v).replace(/\s/g, '').split('')
  return _.chunk(replaceSpaceArr, 4).join(' ').replace(/,/g, '')
})
